package com.sky.controller.admin;

import com.alibaba.druid.sql.PagerUtils;
import com.alibaba.fastjson.JSON;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能：
 * 作者：yedou
 * 日期：2025/3/5 19:11
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = ("套餐相关接口"))
@Slf4j
public class SetmealController {

    @Resource
    private SetmealService setmealService;

    @ApiOperation("新增套餐")
    @PostMapping("/save")
    @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.save.setmealDTO:{}", JSON.toJSON(setmealDTO));
            }
            setmealService.saveWithDish(setmealDTO);
            return Result.success();
        }catch (Exception e){
            log.error("新增套餐错误");
            return Result.error("新增套餐失败");
        }
    }
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.page.setmealPageQueryDTO:{}", setmealPageQueryDTO);
            }

            return Result.success(setmealService.pageQuery(setmealPageQueryDTO));
        }catch (Exception e){
            log.error("分页查询错误");
            return Result.error("分页查询失败");
        }
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.delete.ids:{}", ids);
            }
            setmealService.deleteBatch(ids);
            return Result.success();
        }catch (Exception e){
            log.error("批量删除套餐错误:{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id){


        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.getById.id:{}", id);
            }
            SetmealVO setmealVO = setmealService.getByIdWithDish(id);
            return Result.success(setmealVO);
        }catch (Exception e){
            log.error("根据id查询套餐:{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation("修改套餐")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.update.setmealDTO:{}", JSON.toJSON(setmealDTO));
            }
            setmealService.update(setmealDTO);
            return Result.success();
        }catch (Exception e){
            log.error("修改套餐:{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id){
        try {
            if(log.isInfoEnabled()){
                log.info("SetmealController.update.startOrStop:{},{}", status, id);
            }
            setmealService.startOrStop(status, id);
            return Result.success();
        }catch (Exception e){
            log.error("套餐起售停售:{}",e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}